package com.sbeam.service;

import com.sbeam.mapper.FactoryMapper;
import com.sbeam.mapper.GamesMapper;
import com.sbeam.mapper.SortMapper;
import com.sbeam.pojo.Factory;
import com.sbeam.pojo.Games;
import com.sbeam.pojo.Sort;
import com.sbeam.vo.PageResult;
import com.sbeam.vo.SearchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SearchServiceImpl implements SearchService{

    private String serviceURL = "http://localhost:8888/images";

    @Autowired
    private GamesMapper gamesMapper;

    @Autowired
    private FactoryMapper factoryMapper;

    @Autowired
    private SortMapper sortMapper;

    @Override
    public PageResult findByName(PageResult pageResult) {
        Integer id = pageResult.getId();
        String key = pageResult.getQuery();
        List<SearchVO> list = new ArrayList<>();
        if (key==null || "null".equals(key)){
            key="";
        }
        Map map = new HashMap();
        map.put("key","%"+key+"%");
        map.put("start",(pageResult.getPageNum()-1)* pageResult.getPageSize());
        map.put("pageSize",pageResult.getPageSize());
        if (id==1){
            List<Games> gamesList = gamesMapper.findGamesByName(map);
            int total = gamesMapper.findTotal((String)map.get("key"));
            pageResult.setTotal((long) total);
            for (Games game:gamesList){
                List<String> imageURL = new ArrayList<>();
                if (game.getImages()!=null && !game.getImages().equals("")){
                    String[] gameImages = game.getImages().split(",");
                    List<String> imageList = new ArrayList<>(Arrays.asList(gameImages));
                    for (String image : imageList) {
                        String concat = serviceURL.concat(image);
                        imageURL.add(concat);
                    }

                }
                Double price = Double.valueOf(game.getPrice()/100);
                Sort sort = sortMapper.getSortById(game.getParentId());
                Date created = game.getCreated();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String date = format.format(created);
                SearchVO searchVO = new SearchVO();
                searchVO.setId(game.getId())
                        .setName(game.getName())
                        .setImage(imageURL)
                        .setPriceOrScore("原价 ￥："+price)
                        .setSortOrCount(sort.getSort())
                        .setPublishDate(date);
                list.add(searchVO);
            }
        }else if (id==2){
            List<Factory> factoryList = factoryMapper.findFactoryByName(map);
            int total = factoryMapper.findTotal((String)map.get("key"));
            pageResult.setTotal((long) total);
            for (Factory factory:factoryList){
                List<String> imageURL = new ArrayList<>();
                if (factory.getImages()!=null&&!"".equals(factory.getImages())){
                    String[] factoryImages = factory.getImages().split(",");
                    List<String> imageList = new ArrayList<>(Arrays.asList(factoryImages));
                    for (String image:imageList){
                        String concat = serviceURL.concat(image);
                        imageURL.add(concat);
                    }
                }
                SearchVO searchVO = new SearchVO();
                int count = (gamesMapper.getGamesByFactoryId(factory.getId())).size();
                searchVO.setId(factory.getId())
                        .setName(factory.getFactoryName())
                        .setImage(imageURL)
                        .setSortOrCount("旗下游戏数量："+count)
                        .setPriceOrScore("综合评分："+4.25);
                list.add(searchVO);
            }
        }
        pageResult.setRows(list);
        return pageResult;
    }
}
